import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from "vuex-persistedstate"
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list:[],
    obj:{},
    flag:false,
    sum:0,
    sum2:0
  },
  getters: {
  },
  mutations: {
    storage(state,value){
      let res = state.list.findIndex(item => item.id === value.id);
      if(res == -1){
        Vue.set(value,"num",1)
        state.list.push(value);
      }else{
        state.list[res].num++;
      }
    },
    shujv(state,value){
      state.obj = value
    },

    CHANGE(state,value){
      state.list[value[2]].num = value[1];
    },
    CCC(state,value){
      console.log(value);
      state.list.forEach(item => {
        item.stockout_reserved = value
        console.log(item.stockout_reserved,state.flag);
      })
    },
    ZHUANGT(state,value){
    state.list[value[2]].stockout_reserved = !value[1]
    console.log(state.list[value[2]].stockout_reserved);
    if(state.list[value[2]].stockout_reserved){
      state.sum = state.list[value[2]].num * state.list[value[2]].price
      state.sum2 += state.sum 
      console.log(state.sum);
    }else{
      state.sum = state.list[value[2]].num * state.list[value[2]].price
      state.sum2 -= state.sum 
    }
  },
},
  actions: {
    chang(context,value){
      let a = context.state.list.findIndex(item => item.id == value[0])
      value.push(a)
      // console.log( context.state,value);
      context.commit("CHANGE",value)
    },
    ccc(ctx,value){
      ctx.commit("CCC",value)
    },
    zhuangt(ctx,value){
      let a = ctx.state.list.findIndex(item => item.id == value[0])
      value.push(a)
      ctx.commit("ZHUANGT",value)
    }
  },
  modules: {
  },
  plugins:[createPersistedState({
    key:'aaa',
    paths:["list"]
    // reducer(val){
    //   return{
    //     'aa':val.list
    //   }
    // }
  })]
})
